//加载信息
$.post({
    url: './UserData',
    success: function (data) {
        if (data.code == 0) {
            $('#username').text(data.result.UserName)
            $('#userid').text(data.result.UserID)
            $('#userid1').val(data.result.UserID)
            $('#MenuNum').text('发布数：' + data.result.MenuNum)
            $('#Collection').text('收藏数：' + data.result.Collection)
        }
    }
})

//选项点击事件
$('.chooses').click(function () {
    $('.choosess').children('li').children('a').attr('class', 'nav-link')
    $(this).children().attr('class', 'nav-link active')
    $('.yuan').css('display', 'none')
    $('#' + $(this).children().data().choose).css('display', 'block')
})

//信息修改
var re_usn = function () {
    console.log($("#picture")[0].files[0]);
    var username = $('#username1').val()
    if (username) {
        $.post({
            url: './UserName_change',
            data: {
                UserName: username
            },
            success: function (data) {
                if (data.code == 0) {
                    if ($("#picture")[0].files[0]) {
                        let picture = new FormData()
                        picture.append('file', $("#picture")[0].files[0])
                        $.post({
                            url: './picture',
                            data: picture,
                            contentType: false,
                            processData: false,
                            success: function (data) {
                                if (data.code == 0) {
                                    $("#toast1_text").text('用户名已修改修改')
                                    $("#toast1").attr('class', 'toast align-items-center text-bg-primary border-0 fade show')
                                }
                            }
                        })
                    }
                    else {
                        $("#toast1_text").text('用户名已修改修改')
                        $("#toast1").attr('class', 'toast align-items-center text-bg-primary border-0 fade show')

                    }
                } else {
                    $("#toast1_text").text('修改失败')
                    $("#toast1").attr('class', 'toast align-items-center text-bg-primary border-0 fade show')
                }
            }
        })
    } else {
        $("#toast1_text").text('用户名不能为空')
        $("#toast1").attr('class', 'toast align-items-center text-bg-primary border-0 fade show')
    }
}

//修改密码
var re_pas = function () {
    let old_pas = $("#old_pas")
    let new_pas = $("#new_pas")
    let new_pass = $("#new_pass")
    if (old_pas.val() && new_pas.val() && new_pass.val()) {
        if (new_pas.val() == new_pass.val()) {
            $.post({
                url: './change_pas',
                data: {
                    old_pas: old_pas.val(),
                    new_pas: new_pas.val()
                },
                success: function (data) {
                    if (data.code == 0) {
                        $("#toast1_text").text('修改成功')
                        $("#toast1").attr('class', 'toast align-items-center text-bg-primary border-0 fade show')
                    } else {
                        $("#toast1_text").text('旧密码错误')
                        $("#toast1").attr('class', 'toast align-items-center text-bg-primary border-0 fade show')
                    }
                }
            })
        } else {
            $("#toast1_text").text('两次密码不同')
            $("#toast1").attr('class', 'toast align-items-center text-bg-primary border-0 fade show')
        }
    } else {
        console.log(2222);
        $.each([old_pas, new_pas, new_pass], function (index, value) {
            if (!$(value).val()) $(value).prev().css('color', 'red')
        })
    }

}